package com.example.zyfypt516wh.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.bean.ResBean;
import com.example.zyfypt516wh.iface.RegisterListener;
import com.example.zyfypt516wh.model.RegisterModel;

public class RegisterActivity extends AppCompatActivity {


    private EditText etuser, etpass, etpass2, etname, ettel;
    private Button btncamer, btnregitster, btnreturn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
    }


    private final RegisterListener listener = new RegisterListener() {
        @Override
        public void onResponse(String registermsg) {
            System.out.println("----注册结果:" + registermsg);
            if (registermsg.contains("1")) {
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                finish();
            } else if (registermsg.contains("已存在")) {
                Toast.makeText(RegisterActivity.this, "注册失败，用户名已存在", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            System.out.println("----注册失败:" + msg);
            Toast.makeText(RegisterActivity.this, "注册失败: " + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private void initViews() {
        etuser = findViewById(R.id.etuser);
        etpass = findViewById(R.id.etpass);
        etpass2 = findViewById(R.id.etpass2);
        etname = findViewById(R.id.etname);
        ettel = findViewById(R.id.ettel);
        btncamer = findViewById(R.id.btncamer);
        btnregitster = findViewById(R.id.btnregitster);
        btnreturn = findViewById(R.id.btnreturn);
        btnregitster.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View view) {

                register();//注册代码
            }
        });
        btncamer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(RegisterActivity.this, "功能待开发", Toast.LENGTH_SHORT).show();
            }
        });

        btnreturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                finish();
            }
        });
    }
    private void register(){
        String struser = etuser.getText().toString();
        String strpass = etpass.getText ().toString() ;
        String strpass2 = etpass2. getText().toString();
        String strname = etname. getText().toString() ;
        String strtel =ettel.getText().toString() ;
        if (TextUtils.isEmpty(struser)) {
            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(strpass)) {
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(strname)) {
            Toast.makeText(this,"姓名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(strpass2)){
            Toast.makeText(this,"确认密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(strtel)) {
            Toast.makeText(this, "电话不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!strpass.equals(strpass2)) {
            Toast.makeText(this, "两次密码不一致",Toast.LENGTH_SHORT).show();
            return;
        }
        RegisterModel registermodel=new RegisterModel();
        registermodel.getRegisterResult(struser, strpass,strtel,listener);
    }
}

